package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import com.itheima.health.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CheckItemServiceImpl
 * @Description TODO
 * @Author ly
 * @Company 深圳黑马程序员
 * @Date 2019/10/13 9:54
 * @Version V1.0
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    // 会员
    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;


    @Override
    public Member findMemberByTelephone(String telephone) {
        return memberDao.findMemberByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        // 对密码进行加密保存
        /*if(member.getPassword()!=null){
            member.setPassword(new BCryptPasswordEncoder().encode(member.getPassword()));
        }*/
        memberDao.add(member);
    }

    @Override
    public List<Integer> findMemberCountByRegTime(List<String> monthsList) {
        // 组织查询结果
        List<Integer> memberCountList = new ArrayList<>();
        for (String month : monthsList) {
            String regTime = month+"-31"; // 已知当前月，查询当前月的最后1天
            Integer memberCount = memberDao.findMemberCountByRegTime(regTime);
            memberCountList.add(memberCount);
        }
        return memberCountList;
    }


    /*
    * actor:zhangKai
    * 10-27
    * */
    //根据性别划分会员数量统计图
    @Override
    public List<Map<String, Object>> findBySexCount() {
        List<Map<String,Object>> mapList=memberDao.findBySexCount();

        return mapList;
    }
    //根据年龄段划分会员数量统计
    @Override
    public List<Map<String, Object>> getAgeMemberReport() {
        return memberDao.findByAgeCount();
    }

    @Override
    public List<Integer> findIncreasedMemberCount(List<String> monthsList) {
        List<Integer> countList = new ArrayList<>();
        for (String month : monthsList) {
            int count=memberDao.findIncreasedMemberCount(month);
            countList.add(count);
        }
        return countList;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //使用mybatis的分页插件助手
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());

       Page<Member> page =  memberDao.findPage(queryPageBean.getQueryString());

        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public Member findById(Integer id) {
        return memberDao.findById(id);
    }

    @Override
    public void edit(Member member) {
        memberDao.edit(member);
    }

    @Override
    public void deleteById(Integer id) {
        //删除两种方案
        //1.先删除关联数据，然后再删除表中的数据

        //2.先查询是否有关联数据，有关联数据不让删除，没有关联数据直接删除
        long count = orderDao.findOrderByMemberId(id);
        if (count > 0){
            throw new RuntimeException("有关联数据，不能删除!!!");
        }
        memberDao.deleteById(id);
    }
}
